/**
 * 根据开发环境还是生产环境来配置ajax请求
 *  开发环境为模拟数据
 *  生产环境为axios调接口
 *   url只需传入config里边接口对应的变量名
 *
 */
import axios from 'axios';
import Qs from 'qs';
import config from './config'
import * as MOCKDATA from './mockData'

export const FETCH = (config.NODE_ENV === 'development' ? dev : pro)
//模拟ajax请求
function dev(type = 'POST', url = '', data = {}) {
    return new Promise((resolve, reject) => {
        // Object.keys(config.APIS).forEach((item, index) => {
        //     if (url === item) {
                resolve(MOCKDATA[url]);
        //     }
        // })
    })
}
//正式ajax请求
function pro(type = 'POST', url = '', data = {}) {
    let subUrl = config.APIS[url];
    return new Promise((resolve, reject) => {
        axios({
            url: subUrl,
            method: type,
            baseURL: config.HOST_NAME,
            　
            transformRequest: [function(data) {
                return Qs.stringify(data);
            }],
            transformResponse: [function(data) {
                // 这里提前处理返回的数据
                return data;
            }],
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            },
            data: data,
            timeout: 1000,
            // `onUploadProgress` allows handling of progress events for uploads
            onUploadProgress: function(progressEvent) {
                // Do whatever you want with the native progress event
            },

            // `onDownloadProgress` allows handling of progress events for downloads
            onDownloadProgress: function(progressEvent) {
                // Do whatever you want with the native progress event
            }

        })
    })
}
